package com.isorf.controller;

import com.github.pagehelper.PageInfo;
import com.isorf.pojo.ResponseResult;
import com.isorf.pojo.User;
import com.isorf.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/userList")  // 查询用户列表
    public ResponseResult<List<User>> userList() {
        List<User> userList = userService.userList();
        if (userList == null||userList.size()==0) {
            return ResponseResult.failure(ResponseResult.FAILED, "查询用户列表失败");
        }else {
            return ResponseResult.success(userList);
        }

    }
    //根据用户id修改用户的禁用状态
    @PostMapping("/updateDisable")
    public ResponseResult<Boolean> updateDisable(int id) {
        int result = userService.updateDisable(id);
        if(result>0){
            return ResponseResult.success(ResponseResult.SUCCESS,"修改成功", true);
        }else {
            return ResponseResult.failure(ResponseResult.FAILED,"修改失败");
        }
    }


    //分页查询
    @PostMapping("/pageList")
    //pageNum 是第几页  pageSize 是每页显示多少条数据
    public ResponseResult<PageInfo<User>> pageList(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5") int pageSize){
        PageInfo<User> pageInfo = userService.pageList(pageNum, pageSize);
        if(pageInfo == null || pageInfo.getList().size()==0){
            return ResponseResult.failure(ResponseResult.FAILED,"查询失败");
        }else {
            return ResponseResult.success(ResponseResult.SUCCESS,"查询成功", pageInfo);
        }
    }
}
